(function (window, $, undefined) {
  'use strict';

  var dictData = {
    state: {

    },
    cityId: {

    }
  }

  var typeData = {};

  main(function () {

    var $tableAutoType = $('#table-type');
    util.jqGrid($tableAutoType, {
      url: '/json/test.json',
      datatype: 'json',
      autowidth: true,
      height: $(window).height() - 350,
      multiselect: true,
      multiboxonly: true,
      // shrinkToFit: false,
      colModel: [
        { name: 'kid', key: true, hidden: true },
        { label: 'ID', name: 'kid', width: 50 },
        { label: '城市', name: 'cityId', width: 150 },
        { label: '用车类型', name: 'name', width: 120 },
        { label: '状态', name: 'state', width: 80 },
        { label: '录入时间', name: 'createDate', width: 110 }
      ],
      loadComplete: function (res) {
        typeData = {};
        if (res && res.data) {
          $.each(res.data, function (_, item) {
            typeData[item.kid] = item;
          });
        }
      },
      pager: '#pager-type'
    });

    // 获取城市列表
    util.ajax(ajaxURL.ORGANIZATION + '/city/getCityList?pageNum=1&pageSize=99999', '', 'GET', util.resCallback(function (res) {
      util.selectHtml($('[name="cityId"]'), res.data, 'cityId', 'name', '请选择');
      $.each(res.data, function (_, item) {
        dictData.cityId[item.cityId] = item.name;
      });
      searchTable();
    }));

    // util.getDict('licenseState', 'licenseState').then(function (data) {
    //   util.selectHtml('[name="licenseState"]', data, 'value', 'name', '请选择');
    //   $.each(data, function (_, item) {
    //     dictData.licenseState[item.value] = item.name;
    //   });
    // });

    function searchTable() {
      var data = util.formData('#form-search');
      util.searchGrid($tableAutoType, data);
    }

    $('btn-search').click(searchTable);

    var $layerType = $('#layer-type-detail');
    var $formType = $('#form-type-detail');
    var $saveType = $('#type-save');

    // 新增操作
    $('#btn-add').click(function () {
      $layerType.find(':input').removeAttr('disabled');
      util.layerOpen({
        title: '新增用车类型',
        content: $layerType,
        area: ['500px', ''],
        cancel: function (index) {
          layer.confirm('确定退出当前页面？', {
            btn: ['确定', '再想一想'],
            yes: function (idx) {
              layer.close(index);
              layer.close(idx);
            }
          });
          return false;
        }
      });
    });

    // 修改操作
    $('#btn-edit').click(util.oneCheck($tableAutoType, function (id) {

      $layerType.find(':input').removeAttr('disabled');

      var data = typeData[id];
      util.fillForm($formType, data);

      util.layerOpen({
        title: '修改用车类型',
        content: $layerType,
        area: ['500px', '']
      });
    }));

    $('#type-save').click(function () {

      // if(util.chechForm($formSupplier)) {
        var data = util.formData($formType);

        // data.kid 来决定是什么操作，有为修改，无为新增
        var url = ajaxURL.ORGANIZATION + (data.kid ? '/auto/updateAutoType' : '/auto/addAutoType');
        util.ajax(url, JSON.stringify(data), 'POST', util.resCallback(function (res) {
          layer.closeAll();
          util.tip.success('操作成功');
          $tableDriver.trigger('reloadGrid');
        }));

      // }else {
      //   util.tip.warn('请将数据填写完整');
      // }

    });

  });


})(window, jQuery);
